using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class FireSlider : MonoBehaviour
{
	public static Delegate1Args<float> OnSliderValueChange;

	public static Delegate0Args OnSliderRelease;

	[SerializeField]
	private Image m_Background;

	[SerializeField]
	private Image m_SliderButton;

	private RectTransform m_SliderTrans;

	private float m_MaxPower;

	[SerializeField]
	private Color m_MinColor;

	[SerializeField]
	private Color m_MaxColor;

	[SerializeField]
	private Animator m_Animator;

	private float m_LastValue;

	private float m_FadeTime = 0.2f;

	public float lastValue => m_LastValue;

	private void Awake()
	{
		m_SliderTrans = m_SliderButton.rectTransform;
		m_MaxPower = m_SliderTrans.rect.height - 10f;
		if (!m_Animator)
		{
			m_Animator = GetComponent<Animator>();
		}
	}

	private void Start()
	{
		m_Background.color = m_MinColor;
	}

	public void CalculateSliderOffset(Vector3 delta)
	{
		Vector3 position = BaseUIController.GetUICamera().WorldToScreenPoint(m_SliderButton.transform.position);
		position.y += delta.y;
		m_SliderButton.transform.position = BaseUIController.GetUICamera().ScreenToWorldPoint(position);
		Vector3 localPosition = m_SliderTrans.localPosition;
		localPosition.y = Mathf.Clamp(localPosition.y, 0f - m_MaxPower, 0f);
		m_SliderTrans.localPosition = localPosition;
		float value = localPosition.y / (0f - m_MaxPower);
		DiscreteValue(ref value);
		m_Background.color = Color.Lerp(m_MinColor, m_MaxColor, value);
		if (OnSliderValueChange != null && m_LastValue != value)
		{
			OnSliderValueChange(value);
		}
		m_LastValue = value;
	}

	public void OnFireSliderDrag(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		CalculateSliderOffset(pointerEventData.delta);
	}

	public void OnFireSliderDragEnd(BaseEventData data)
	{
		if (m_LastValue > 0.02f && OnSliderRelease != null)
		{
			OnSliderRelease();
			m_LastValue = 0f;
		}
		iTween.MoveTo(m_SliderButton.gameObject, iTween.Hash("y", 0, "time", m_FadeTime, "islocal", true));
		iTween.ColorTo(m_Background.gameObject, iTween.Hash("color", m_MinColor, "time", m_FadeTime, "includechildren", false));
	}

	private void DiscreteValue(ref float value)
	{
		value = Mathf.Pow(value, 1.1f);
	}

	public void Hide()
	{
		m_Animator.SetTrigger("Hide");
	}

	public void Show()
	{
		m_Animator.SetTrigger("Show");
	}
}
